home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2986 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Adding Arrays, anyone?
  5. Date: Thu, 25 Jan 1996 12:36:27 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <31075D2B.676E@cmt.lpr.mail.carel.fi>
  8. References: <4e6gpm$dfb@doc.jmu.edu>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. Rachel K. Warren wrote:
  16. > Could somebody give me an example of adding two arrays together?
  17. > Say the first array has four mailboxes with "1" in it to, so when you
  18. > print it out its,"1111" and the second array has four mailboxes with the
  19. > number "2" in it, so that you print it out and its "2222", so I could get
  20. > "3333"?
  21. > I tried something like
  22. > for(counter =0 ; counter != '\0'; counter++)
  23. >  printf("%c",string1[counter] + string2[counter]
  24. > and I got a bunch of garbage on the screen.
  25.  
  26. You have some bugs here. First, you're adding the ASCII codes of the two letters ('1' = 
  27. 0x31, '2' = 0x32 -> 0x63 = 'c'). Second, your loop exit condition is wrong. If you want 
  28. to use ASCIIZ strings, you should write (I use a temporary variable here for making 
  29. the process more clear):
  30.  
  31.     int    tmp;
  32.  
  33.     for (counter = 0; string1[counter] != '\0'; counter++) {
  34.         tmp = (string1[counter] - '0') + (string2[counter] - '0');
  35.         printf("%c", tmp + '0');
  36.     }
  37.  
  38. Another, and faster way, is to use pointers:
  39.  
  40.     char    *p1 = string1;
  41.     char    *p2 = string2;
  42.  
  43.     while (*p1) {
  44.         tmp = (*p1++ - '0') + (*p2++ - '0');
  45.         printf("%c", tmp + '0');
  46.     }
  47.  
  48. However, if you'll one day have the arrays:
  49.  
  50.     char    string1[5] = "5555";
  51.     char    string2[5] = "5555";
  52.  
  53. you will have quite interesting results. So, you should switch using integer arrays:
  54.  
  55.     int    string1[4] = { 5, 5, 5, 5 };
  56.     int    string2[4] = { 5, 5, 5, 5 };
  57.  
  58.     int    string3[4];
  59.  
  60.     #define    DIM(x)    (sizeof(x)/sizeof(x[0]))
  61.  
  62.     for (counter = 0; counter < DIM(string1); counter++)
  63.         printf("%d ", string1[counter] + string2[counter]);
  64.  
  65. This, of course, works only if the arrays are of the same size. Consider the following 
  66. code:
  67.  
  68.     int    string1[] = { 1, 1, 1, 1 };
  69.     int    string2[] = { 2, 2, 2 };
  70.  
  71.     ...
  72.  
  73. What do you think is the output from the for loop?
  74.  
  75. Later,
  76. AriL
  77. -- 
  78. All my opinions are mine and mine alone.
  79.